var express = require("express");
var router = express.Router();

var { userModel } = require("../db/user_model");
var { rideruserModel } = require("../db/rider");
var jwt = require("jsonwebtoken");

// 展示用户信息
router.get("/get_info_login", async (req, res) => {
  let list = await infoModel.find().exec();
  res.send({
    code: 200,
    msg: "展示成功",
    data: list,
  });
});

// 用户进行登录
// router.post("/info_login", async (req, res) => {
//   let body = req.body;
//   console.log(body);
//   let [data] = await infoModel.find(body).exec();
//   console.log(data, "data11111111111111111");
//   console.log("is data", data);

//   if (data) {
//     let token =
//       "Bearer" +
//       " " +
//       jwt.sign({ mobile: data.mobile }, "flag", { expiresIn: 10000 });
//     res.send({
//       code: 200,
//       msg: "登录成功",
//       data,
//       token,
//     });
//   } else {
//     if (!body.passWord) {
//       res.send({
//         code: 201,
//         msg: "用户或者密码错误",
//       });
//     } else {
//       let token =
//         "Bearer" +
//         " " +
//         jwt.sign({ mobile: body.mobile }, "flag", { expiresIn: 10000 });
//       let createData = {
//         userName: "用户" + new Date().getTime(), //用户名
//         mobile: body.mobile, //手机号
//         head: "uploads/info/777.png",
//         zhiwei: " ",
//       };
//       await infoModel.create(createData);

//       res.send({
//         code: 200,
//         msg: "登录成功",
//         createData,
//         token,
//       });
//     }
//   }
// });

/* //   无密码 */
router.post("/phone_login", async (req, res) => {
  let body = req.body;
  console.log(body);
  // let [data] = await userModel.find(body).exec();
  const data = await userModel.find().where({
    mobile: req.body.mobile,
  });
  if (data.length == 0) {
    await userModel.create(req.body);
    let token =
      "Bearer" +
      " " +
      jwt.sign({ mobile: data.mobile }, "flag", { expiresIn: 10000 });
    const data1 = await userModel.find().where({
      mobile: req.body.mobile,
    });
    res.send({
      code: 200,
      msg: "登录成功",
      data: data1[0],
      token,
    });
  } else {
    let token =
      "Bearer" +
      " " +
      jwt.sign({ mobile: body.mobile }, "flag", { expiresIn: 10000 });
    res.send({
      code: 200,
      msg: "登录成功",
      data: data[0],
      token,
    });
  }
});

// 骑手登录注册
router.post("/riderphone_login", async (req, res) => {
  let body = req.body;
  console.log(body);
  // let [data] = await userModel.find(body).exec();
  const data = await rideruserModel.find().where({
    phone: req.body.phone,
  });
  if (data.length == 0) {
    await rideruserModel.create(body);
    let token =
      "Bearer" +
      " " +
      jwt.sign({ phone: data.phone }, "flag", { expiresIn: 10000 });
    const data1 = await rideruserModel.find().where({
      phone: req.body.phone,
    });
    console.log(data1);
    res.send({
      code: 200,
      msg: "注册成功",
      data: data1[0],
      token,
    });
  } else {
    let token =
      "Bearer" +
      " " +
      jwt.sign({ phone: body.phone }, "flag", { expiresIn: 10000 });
    res.send({
      code: 200,
      msg: "登录成功",
      data: data[0],
      token,
    });
  }
});

// 微信一件登录
router.post("/wx_login", async (req, res) => {
  let body = req.body;
  console.log(body);
  // let [data] = await userModel.find(body).exec();
  const data = await userModel.find().where({
    openid: req.body.openid,
  });
  if (data.length == 0) {
    await userModel.create(req.body);
    let token =
      "Bearer" +
      " " +
      jwt.sign({ openid: data.openid }, "flag", { expiresIn: 10000 });
    const data1 = await userModel.find().where({
      openid: req.body.openid,
    });
    res.send({
      code: 200,
      msg: "登录成功",
      data: data1[0],
      token,
    });
  } else {
    let token =
      "Bearer" +
      " " +
      jwt.sign({ openid: body.openid }, "flag", { expiresIn: 10000 });
    res.send({
      code: 200,
      msg: "登录成功",
      data: data[0],
      token,
    });
  }
});

// 跑腿骑手微信一件登录
router.post("/riderwx_login", async (req, res) => {
  let body = req.body;
  console.log(body);
  // let [data] = await userModel.find(body).exec();
  const data = await rideruserModel.find().where({
    openid: req.body.openid,
  });
  if (data.length == 0) {
    await rideruserModel.create(req.body);
    let token =
      "Bearer" +
      " " +
      jwt.sign({ openid: data.openid }, "flag", { expiresIn: 10000 });
    const data1 = await rideruserModel.find().where({
      openid: req.body.openid,
    });
    res.send({
      code: 200,
      msg: "注册成功",
      data: data1[0],
      token,
    });
  } else {
    let token =
      "Bearer" +
      " " +
      jwt.sign({ openid: body.openid }, "flag", { expiresIn: 10000 });
    res.send({
      code: 200,
      msg: "登录成功",
      data: data[0],
      token,
    });
  }
});

module.exports = router;
